import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {
    private Socket socket = null;

    public TcpEchoClient(String serverIp, int serverPort) throws IOException {
        socket = new Socket(serverIp, serverPort);
    }

    public void start() throws IOException {
        System.out.println("客户端启动");
        Scanner scannerConsole = new Scanner(System.in);
        try (InputStream inputStream = socket.getInputStream();
             OutputStream outputStream = socket.getOutputStream()) {
            while (true) {
                //1.从控制体啊输入字符串
                System.out.println("->");
                String request = scannerConsole.next();
                //2.把请求发送给服务器
                PrintWriter printWriter = new PrintWriter(outputStream);
                //使用println带上换行，后续服务器读取请求，就可以用scanner.next来获取
                printWriter.println(request);
                printWriter.flush();
                //3.从服务器读取响应
                Scanner scannerNetWork = new Scanner(inputStream);
                String response = scannerNetWork.next();
                //4.把响应打印出来
                System.out.println(response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws IOException {
        TcpEchoClient client=new TcpEchoClient("127.0.0.1",9090);
        client.start();
    }












}
